<template>
  <div class="sku">
    <el-form label-width="100px">
      <el-form-item label="sku名称">
        <el-input placeholder="SKU名称" v-model="skuForm.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格（元）">
        <el-input placeholder="价格（元）" type="number" v-model="skuForm.price"></el-input>
      </el-form-item>
      <el-form-item label="重量（克）">
        <el-input placeholder="重量（克）" type="number" v-model="skuForm.weight"></el-input>
      </el-form-item>
      <el-form-item label="sku描述">
        <el-input placeholder="sku描述" type="areatext" v-model="skuForm.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性" v-if="attrArr.length > 0">
        <el-form :inline='true' label-width="70px">
          <el-form-item v-for="item in attrArr" :label="item.attrName" :key="item.id">
            <el-select v-model="item.attrIdAndValueId"> 
              <el-option v-for="opt in item.attrValueList" :label="opt.valueName" :key="opt.id"
                :value="`${item.id}:${opt.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性" v-if="saleArr.length > 0">
        <el-form :inline='true' label-width="70px">
          <el-form-item v-for="item in saleArr" :label="item.saleAttrName" :key="item.id">
            <el-select v-model="item.saleIdAndValueId">
              <el-option v-for="opt in item.spuSaleAttrValueList" :label="opt.saleAttrValueName" :key="opt.id"
                :value="`${item.id}:${opt.id}`"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片名称">
        <el-table border :data="imgArr" ref="table">
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="图片">
            <template #="{ row, $index }">
              <img :src="row.imgUrl" alt="" style="height: 100px;">
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button type="primary" size="small" @click="handler(row)">设置默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveSku">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>
<script setup lang='ts'>
import { ref } from 'vue'
import { spuImageListAPI, spuSaleAttrListAPI, addSkuInfo } from '@/api/product/spu/index.ts'
import { getAttrInfoList } from '@/api/product/attr/index.ts'
import { ElMessage } from 'element-plus';
let $emit = defineEmits(['changeScene'])
//平台属性
let attrArr = ref<any>([]);
//销售属性
let saleArr = ref<any>([]);
//照片的数据
let imgArr = ref<any>([]);
//获取table组件实例
let table = ref<any>();
let skuForm = ref({
  //父组件传递过来的数据
  category3Id: "",//三级分类的ID
  spuId: "",//已有的SPU的ID
  tmId: "",//SPU品牌的ID
  //v-model收集
  skuName: "",//sku名字
  price: "",//sku价格
  weight: "",//sku重量
  skuDesc: "",//sku的描述
  skuAttrValueList: [//平台属性的收集
  ],
  skuSaleAttrValueList: [//销售属性
  ],
  skuDefaultImg: "",//sku图片地址

})
//当前子组件的方法对外暴露
const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: any) => {
  //收集数据
  skuForm.value.category3Id = spu.category3Id;
  skuForm.value.spuId = spu.id;
  skuForm.value.tmId = spu.tmId;
  // 获取平台属性
  let res1 = await getAttrInfoList(c1Id, c2Id, spu.category3Id)
  // 获取照片墙数据
  let res2 = await spuImageListAPI(spu.id)
  // 获取销售属性
  let res3 = await spuSaleAttrListAPI(spu.id)
  attrArr.value = res1.data
  imgArr.value = res2.data
  saleArr.value = res3.data
  console.log(res1, res2, res3);
}
// 设为默认图片点击事件
const handler = (row: any) => {
  //点击的时候,全部图片的的复选框不勾选
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false);
  });
  //选中的图片才勾选
  table.value.toggleRowSelection(row, true);
  //收集图片地址
  skuForm.value.skuDefaultImg = row.imgUrl;
}
// 保存事件
const saveSku = async () => {
  // 整理参数
  // 平台属性
  skuForm.value.skuAttrValueList = attrArr.value.reduce((pre, next) => {
    if (next.attrIdAndValueId) {
      const [attrId, valueId] = next.attrIdAndValueId.split(':')
      pre.push({
        attrId,
        valueId
      })
    }
    return pre
  }, [])
  // 销售属性
  skuForm.value.skuSaleAttrValueList = saleArr.value.reduce((pre, next) => {
    if (next.saleIdAndValueId) {
      const [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
      pre.push({
        saleAttrId,
        saleAttrValueId
      })
    }
    return pre
  }, [])
  let result = await addSkuInfo(skuForm.value)
  const { code, message } = result
  if (code == 200) {
    ElMessage.success('保存成功')
    //通知父组件切换场景为零
    $emit('changeScene', 0)
  } else {
    ElMessage.error(message)
  }
}
// 取消事件
const cancel = () => {
  $emit('changeScene', 0);
}
defineExpose({ initSkuData })


</script>
<style lang="scss" scoped></style>
<style>
.el-form-item__content {
  width: 200px;
}

.el-form-item .el-form-item {
  margin-bottom: 20px;
}
</style>